gh-145629: Expand block inlining to enable LOAD_FAST_BORROW optimization for return ternary expressions#146503
Open
cocolato wants to merge 2 commits intopython:mainfrom
Open
gh-145629: Expand block inlining to enable LOAD_FAST_BORROW optimization for return ternary expressions#146503cocolato wants to merge 2 commits intopython:mainfrom
cocolato wants to merge 2 commits intopython:mainfrom
Conversation
Member
|
To my untrained (for the compiler) eye, this looks fine, but maybe someone else needs to review it. |
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In existing CFG optimization logic, for equivalent conditional code, ternary expressions do not get the
LOAD_FASToptimization applied, unlike standardif/elsestatements:The return in the
if/elsebranch is successfully optimized toLOAD_FAST_BORROW, whereas theelsebranch in the ternary expression can only generate a standardLOAD_FAST.Before being passed to
optimize_load_fast(the entry point for the reference checking phase), the control flow graphs of the two structures exhibit subtle differences:For
if/else, itselsebranch naturally belongs to a single basic block, containing both the reference being loaded and the return instruction that consumes that reference:Because the local reference is consumed directly within this basic block,
optimize_load_fastcan determine that the safety constraint is satisfied and convert it into a prioritizedLOAD_FAST_BORROW.However, for a trinary expression, due to the structural characteristics of the AST, the code generator produces the following CFG, which prevents
optimize_load_fastfrom performing the optimization:So this PR extends the inlining capabilities of
basicblock_inline_small_or_no_lineno_blocks:RETURN_VALUE), we can still safely copy it directly and inline it at the end of the underlying block.LOAD_FASTis not always converted toLOAD_FAST_BORROWin abasicblock#145629